Session 1 Cheat Sheet — R and RStudio Basics

Applied Methods · MA Public Administration

Author

Maxime Walder

Published

September 18, 2026

1. Session objectives

At the end of Session 1, you should be able to:

  • open RStudio and identify the main panes
  • run commands in the console and from a script
  • create objects with the assignment operator <-
  • distinguish basic object classes: numeric, character, logical, factor
  • create and inspect vectors
  • create a simple data frame
  • define and use a simple function
  • load a package with library()
  • inspect a dataset with View(), head(), str(), and summary()

2. R versus RStudio

R is the language and statistical software that performs the calculations.

RStudio is an integrated development environment that makes R easier to use. In practice, we open R through RStudio.

Recommended installation order:

  1. Install R from CRAN: https://cran.r-project.org/
  2. Install RStudio Desktop from Posit: https://posit.co/download/rstudio-desktop/
  3. Open RStudio and check that the console starts correctly.

3. RStudio panes

Pane Use
Source Write and save scripts or Quarto documents
Console Run R commands interactively
Environment See objects currently stored in memory
History See commands previously executed
Files Navigate folders and files
Plots Display figures
Packages Install, update, and load packages
Help Read documentation

A good habit is to write code in the Source pane and run it from there. This makes your work reproducible.

4. Basic operations

R can be used as a calculator.

2 + 2
[1] 4
10 - 3
[1] 7
5 * 4
[1] 20
20 / 5
[1] 4
sqrt(16)
[1] 4

Parentheses matter.

10 + 5 * 2
[1] 20
(10 + 5) * 2
[1] 30

5. Assignment operator

Use <- to store a value in an object.

age <- 24
city <- "Geneva"
share <- 37.5

Read this as:

age gets the value 24.

Once an object exists, you can reuse it.

age + 1
[1] 25
paste("The city is", city)
[1] "The city is Geneva"

6. Naming objects

Recommended object names:

mean_income <- 5200
number_students <- 28
public_staff_2026 <- 455

Avoid names with spaces, accents, or special symbols.

mean income <- 5200     # bad: contains a space
mean-income <- 5200     # bad: R reads - as subtraction
économie <- 5200        # avoid accents in object names

7. Basic object classes

one_number <- 10
one_text <- "public administration"
one_logical <- TRUE
one_factor <- factor("Geneva")

class(one_number)
[1] "numeric"
class(one_text)
[1] "character"
class(one_logical)
[1] "logical"
class(one_factor)
[1] "factor"

Common classes:

Class Meaning Example
numeric numbers 10, 3.14
character text "Geneva"
logical TRUE/FALSE TRUE, FALSE
factor categorical variable factor("urban")

8. Vectors

A vector contains several values.

communes <- c("Carouge", "Cologny", "Lancy", "Meyrin")
population <- c(22600, 5700, 34400, 27000)

Useful functions:

length(population)
[1] 4
mean(population)
[1] 22425
min(population)
[1] 5700
max(population)
[1] 34400

Extract elements with square brackets.

communes[1]
[1] "Carouge"
population[1:2]
[1] 22600  5700

9. Missing values

In R, missing values are coded as NA.

x <- c(1, 2, 3, NA)
mean(x)
[1] NA
mean(x, na.rm = TRUE)
[1] 2

Many functions have an na.rm = TRUE argument, which means “remove missing values before computing the result”.

10. Data frames

A data frame is a rectangular dataset.

  • Rows are observations.
  • Columns are variables.
municipal_data <- data.frame(
  commune = communes,
  population = population
)

municipal_data
  commune population
1 Carouge      22600
2 Cologny       5700
3   Lancy      34400
4  Meyrin      27000

Inspect a data frame:

head(municipal_data)
  commune population
1 Carouge      22600
2 Cologny       5700
3   Lancy      34400
4  Meyrin      27000
str(municipal_data)
'data.frame':   4 obs. of  2 variables:
 $ commune   : chr  "Carouge" "Cologny" "Lancy" "Meyrin"
 $ population: num  22600 5700 34400 27000
summary(municipal_data)
   commune            population   
 Length:4           Min.   : 5700  
 Class :character   1st Qu.:18375  
 Mode  :character   Median :24800  
                    Mean   :22425  
                    3rd Qu.:28850  
                    Max.   :34400  
nrow(municipal_data)
[1] 4
ncol(municipal_data)
[1] 2

Create a new variable with $.

municipal_data$population_thousands <- municipal_data$population / 1000
municipal_data
  commune population population_thousands
1 Carouge      22600                 22.6
2 Cologny       5700                  5.7
3   Lancy      34400                 34.4
4  Meyrin      27000                 27.0

11. Functions

A function takes input, performs operations, and returns output.

divide_by_4_add_1 <- function(x) {
  x / 4 + 1
}

divide_by_4_add_1(20)
[1] 6

The same function can often be applied to a vector.

divide_by_4_add_1(c(4, 8, 12))
[1] 2 3 4

12. Packages and libraries

A package extends what R can do.

Install a package once:

install.packages("readr")

Load it each time you open a new R session and need it:

library(readr)

Important distinction:

  • install.packages() downloads and installs the package on your computer.
  • library() loads the package into the current R session.

13. Inspect a database

Different commands exist to inspect the data:

View(first_data)
head(first_data)
str(first_data)
summary(first_data)

14. Getting help

Use ? before a function name.

?mean
?read.csv
?data.frame

Or use the Help pane in RStudio.

15. How to read error messages

When you get an error:

  1. Do not panic.
  2. Read the first line of the error message.
  3. Identify the object or function mentioned.
  4. Check spelling and capitalization.
  5. Check whether the object exists in the Environment pane.
  6. Check whether the relevant package has been loaded with library().
  7. Run the script line by line to locate the problem.

Example:

my_budgte + 10

The problem is a spelling error: my_budgte does not exist.

16. Minimal reproducible workflow for this course

For every assignment and group project, try to follow this logic:

# 1. Load packages

# 2. Import data

# 3. Inspect data

# 4. Clean or transform data

# 5. Analyze data

# 6. Produce tables or figures

# 7. Interpret the result

The objective is not only to obtain a result, but to make the result reproducible and understandable.